package com.alameda.service.impl.user;

import com.alameda.common.commonpent.redis.api.RedisCacheTemplate;
import com.alameda.common.utils.ExcelUtils;
import com.alameda.common.utils.ObjectUtils;
import com.alameda.common.utils.PubUtils;
import com.alameda.mapper.user.AdUserMapper;
import com.alameda.pojo.user.AdUserDO;
import com.alameda.user.bo.AdUserBO;
import com.alameda.user.dto.AdUserDTO;
import com.alameda.user.vo.AdUserVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 用户 业务逻辑层 具体实现扩展
 *
 * @author Lv
 * @date 2024/2/19
 */
@Slf4j
@Service
@Primary
public class AdUserServiceImplExt extends AdUserServiceImpl {

    @Autowired
    private AdUserMapper adUserMapper;

    /**
     * 查询用户列表
     *
     * @param adUserDTO 用户信息
     * @return
     */
    @Override
    public IPage<AdUserVO> loadUser(AdUserDTO adUserDTO) {
        IPage<AdUserDO> iPage = new Page<>(adUserDTO.getPageNum(), adUserDTO.getPageSize());
        return adUserMapper.listUserData(iPage, adUserDTO.getNickname(), adUserDTO.getEmail(), adUserDTO.getOrganizationId());
    }

    /**
     * 导出excel
     *
     * @param adUserDTO 用户信息
     */
    @Override
    public void export(AdUserDTO adUserDTO) {
        List<AdUserBO> adUserBOList = getExportData(adUserDTO);
        ExcelUtils.export(adUserBOList, AdUserBO.class, "用户信息");
    }

    /**
     * 获取导出数据
     *
     * @param adUserDTO 用户信息
     * @return
     */
    private List<AdUserBO> getExportData(AdUserDTO adUserDTO) {
        IPage<AdUserVO> pageData = loadUser(adUserDTO);
        List<AdUserVO> dataList = pageData.getRecords();

        if (PubUtils.isEmpty(adUserDTO.getIdList())) {
            // 获取当前页面数据
            return ObjectUtils.convertList(dataList, AdUserBO.class);
        } else {
            // 获取选中数据
            return dataList.stream()
                    .filter(item -> adUserDTO.getIdList().contains(item.getId()))
                    .map(item -> ObjectUtils.convert(item, AdUserBO.class))
                    .collect(Collectors.toList());
        }
    }

    /**
     * 删除组织下的用户
     *
     * @param organizationIdList 组织id集合
     * @return
     */
    @Override
    public Boolean removeByOrganizationIdList(List<Long> organizationIdList) {
        return this.remove(Wrappers.lambdaQuery(AdUserDO.class)
                .in(AdUserDO::getOrganizationId, organizationIdList));
    }

    @Autowired
    private RedisCacheTemplate redisCacheTemplate;

    /**
     * 处理管理端token续期
     *
     * @param token 登录令牌
     * @return
     */
    @Override
    public Boolean dealAdUserTokenRenewal(String token) {
        AdUserDO adUserDO = findByToken(token);
        if (PubUtils.isNull(adUserDO)) {
            // do nothing
        } else {
            log.info("处理用户[{}]登录续期", adUserDO.getUsername());
            // 写入redis续期
            redisCacheTemplate.setCacheObject(token, token, 5, TimeUnit.HOURS);
        }
        return Boolean.TRUE;
    }
}
